home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / tekamiga.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-11  |  46.2 KB  |  2,000 lines

  1. #include <stdio.h>
  2.  
  3. #include <exec/types.h> 
  4. #include <exec/ports.h>
  5. #include <graphics/gfxbase.h>
  6. #include <graphics/gfx.h>
  7. #include <graphics/view.h>
  8. #include <graphics/rastport.h>
  9. #include <graphics/layers.h>
  10. #include <graphics/display.h>
  11. #include <graphics/gfxmacros.h>
  12. #include <intuition/intuition.h>
  13. #include <libraries/dos.h>
  14. #include <libraries/dosextens.h>
  15. #include <workbench/workbench.h>
  16. #include <workbench/startup.h>
  17. #include <workbench/icon.h>
  18.  
  19. #include <auxcfg.h>
  20. #include <spec.h>
  21. #include <mdllib.h>
  22.  
  23. #define BMHDChunk 1
  24. #define CMAPChunk 2
  25. #define BODYChunk 3
  26. #define CAMGChunk 4
  27. #define Unknown   5
  28.  
  29.  
  30. #define TEKPIPENAME0 "ram:liserp0"
  31. #define TEKPIPENAME1 "ram:liserp1"
  32. #define TEKPIPENAME2 "ram:liserp2"
  33. #define TEKPIPENAME3 "ram:liserp3"
  34.  
  35. #define TMPGCURSOR   "t:gcursor"
  36. #define ASHBDRY      "t:ashbdry"
  37.  
  38. #define _TEKXMAX 4096
  39. #define _TEKYMAX 3120
  40.  
  41. extern struct WBStartup *WBenchMsg;
  42. extern struct IconBase *IconBase;
  43.  
  44. BOOL close_me;
  45.  
  46. int   cx,cy,cb1,cb2,
  47.       esc,fdstdin,fdstdout,
  48.       ink=2,
  49.        opt=1,
  50.        prt_passes=2,
  51.        prt_lsbmsb=1,
  52.        x_prt_siz=1024,
  53.        y_prt_siz=1024,
  54.        aposx=0,
  55.        aposy=0,
  56.        fdprinter=0,
  57.        pribufptr=0,
  58.        pribufsiz=8190,
  59.       paper=0,
  60.       ptr=0,
  61.       bufsiz=200000,
  62.       nohco=FALSE,
  63.       msizex, msizey,
  64.       sizex, sizey,
  65.       sizex_1, sizey_1,
  66.       sizex_2, sizey_2,
  67.       thickx=1,
  68.       thicky=1,
  69.       txtangle=0,
  70.       sinphi=0,
  71.       cosphi=262144,
  72.       gramode=0,
  73.       flg_half=FALSE,
  74.       hcothick=FALSE,
  75.       bufferonfile=FALSE;
  76.  
  77. int            fdbuffer, fdptr;
  78. unsigned char  lastchar;
  79.  
  80. unsigned char   *tekname,
  81.                 prt_init[20]="\x1B@",
  82.                 prt_gra[20]="\x1BZ",
  83.                 prt_halflf[20]="\x1B\x33\x02\n",
  84.                 prt_lf[20]="\x1B\x33\x14\n",
  85.                 prt_fin[20]="\x1B@\x0C",
  86.                 *primaryfont,
  87.                 *secondaryfont,
  88.                 *defaultprinter,
  89.                 *systemcommand,
  90.                 *bufferfile,
  91.               *buf,
  92.               *printerbuf,
  93.               *tekbuf,
  94.               *font_1,
  95.               *fontdesc_1,
  96.               *font_2,
  97.               *fontdesc_2,
  98.               *fontfile, *fontdesc;
  99.  
  100. int            *prop_1,
  101.                fontwidth_1, fontheight_1,
  102.                firstchar_1, offschar_1,
  103.                *prop_2,
  104.                fontwidth_2, fontheight_2,
  105.                firstchar_2, offschar_2,
  106.                *proptab,
  107.                fontwidth, fontheight,
  108.                firstchar, offschar;
  109.  
  110. int            prop_flag = 1;
  111. int            baseline = 0;
  112.  
  113. struct CMAPC          /* Definition des CMAP-Chunks               */
  114. {
  115.  int anz;             /* Anzahl der Farben                        */
  116.  struct ColorRegister /* Definition einer Farbe                   */
  117.  {
  118.   UBYTE red;
  119.   UBYTE green;
  120.   UBYTE blue;
  121.  } Color[64];         /* lieber zuviel als zuwenig                */
  122. };
  123.  
  124. struct FORMC          /* Definition des Anfang-Chunks             */
  125. {
  126.  char  FORMName[4];   /* handelt es sich um ein IFF-Format?       */
  127.  long  len;           /* Gesamte Laenge des Files                 */
  128.  char  ILBM[4];       /* handelt es sich um ILBM-Format?          */
  129. };
  130.  
  131. struct BMHDC          /* Definition des BMHD                      */
  132. {
  133.  char  BMHDName[4];
  134.  long  lenBMHD;       /* Laenge des BMHD-Chunks                   */
  135.  short Width;         /* Breite des Bildes                        */
  136.  short Height;        /* Hoehe des Bildes                         */
  137.  short xpos;          /* x-Position des Bildes                    */
  138.  short ypos;          /* y-Position des Bildes                    */
  139.  char  Depth;         /* Anzahl der BitPlanes                     */
  140.  char  mask;          /* Mask-Bit                                 */
  141.  char  compression;   /* Bild 'compressed'?                       */
  142.  char  null;
  143.  short color;
  144.  char  xaspect;
  145.  char  yaspect;
  146.  short PWidth;
  147.  short PHeight;
  148. };
  149.  
  150. struct CHUNKID        /* hier werden die zu analysierenden Bytes  */
  151. {                     /* abgelegt                                 */
  152.  char  Name[4];       /* Name des Chunks                          */
  153.  int   len;           /* Laenge des Chunks                        */
  154. };
  155.  
  156. struct HEADER         /* diese Struktur enthaelt die Zeichenfolge */
  157. {                     /* um den Chunk zu erkennen.                */
  158.  char bmhd[4];
  159.  char cmap[4];
  160.  char body[4];
  161.  char camg[4];
  162. };
  163.  
  164. struct CAMGC          /* Definition des CAMG                      */
  165. {
  166.  char CAMGName[4];
  167.  long lenCAMG;
  168.  long ViewModes;
  169. };
  170.  
  171. struct CMAPC CMAPl;
  172. struct FORMC FORMl;
  173. struct BMHDC BMHDl;
  174. struct CHUNKID CHUNKid;
  175. struct FORMC FORMs = {'F','O','R','M',0,'I','L','B','M'};
  176. struct BMHDC BMHDs = {'B','M','H','D',20,0,0,0,0,0,2,0,0,0,10,11,0,0};
  177. struct CAMGC CAMGs = {'C','A','M','G',4,0};
  178. struct HEADER Header=
  179. {
  180.  'B','M','H','D',    /* BMHD-Chunk                                */
  181.  'C','M','A','P',    /* CMAP-Chunk                                */
  182.  'B','O','D','Y',    /* BODY-Chunk                                */
  183.  'C','A','M','G',    /* CAMG-Chunk                                */
  184. };
  185.  
  186. char cmap[]="CMAP";
  187. char body[]="BODY";
  188. char form[]="FORM";
  189. char ilbm[]="ILBM";
  190.  
  191.  
  192. /* ---------------------------------------------
  193.          User Callback functions
  194.    --------------------------------------------- */
  195. float frline(stream)
  196. FILE *stream;
  197. {
  198. char s[80];
  199.  
  200.    fgets(s,80,stream);
  201.    return(atosf(s));
  202. }
  203.  
  204. void Cursor()
  205. {
  206. int x,y,xx,
  207.       leftmargin,
  208.       bottommargin,
  209.       rightmargin,
  210.       topmargin,
  211.       _win_flg;
  212. float tmin,
  213.       tmax,
  214.       ymin,
  215.       ymax,
  216.       real_t,
  217.       real_y,
  218.       yfak,
  219.       tfak,
  220.       xoffset,
  221.       yoffset;
  222. char  s[80];
  223. FILE *fp;
  224.  
  225. getxy(&x,&y);
  226.  
  227. fp=fopen(ASHBDRY,"r");
  228. leftmargin = frline(fp);
  229. bottommargin = frline(fp);
  230. rightmargin = frline(fp);
  231. topmargin = frline(fp);
  232. _win_flg = frline(fp);
  233. tmin = frline(fp);
  234. tmax = frline(fp);
  235. ymin = frline(fp);
  236. ymax = frline(fp);
  237. _tica = frline(fp);
  238. fclose(fp);
  239.  
  240. yfak=(topmargin-bottommargin)/(ymax-ymin);
  241. tfak=(rightmargin-leftmargin)/(tmax-tmin);
  242. xoffset= ((- tmin) * tfak) + leftmargin;
  243. yoffset= ((- ymin) * yfak) + bottommargin;
  244.  
  245. real_t= (x - xoffset)/tfak;
  246. real_y= (y - yoffset)/yfak;
  247. xx= real_t / _tica;
  248.  
  249. sprintf(s,"tek x = %d  tek y = %d\ndata:      x=%f    y=%f\nx-channel = %d\n"
  250.            ,x,y,real_t,real_y,xx);
  251. Help(s);
  252. }
  253.  
  254. void InkPlus()
  255. {
  256. char s[80];
  257.  
  258. ink = ((ink + 1) & 7);
  259. SetAPen(rp,ink);
  260. sprintf(s,"ink  %d\npaper %d\n",ink,paper);
  261. Help(s);
  262. }
  263.  
  264. void InkMinus()
  265. {
  266. char s[80];
  267.  
  268. ink = ((ink - 1) & 7);
  269. SetAPen(rp,ink);
  270. sprintf(s,"ink  %d\npaper %d\n",ink,paper);
  271. Help(s);
  272. }
  273.  
  274. void PaperPlus()
  275. {
  276. char s[80];
  277.  
  278. paper = ((paper + 1) & 7);
  279. sprintf(s,"ink  %d\npaper %d\n",ink,paper);
  280. Help(s);
  281. }
  282.  
  283. void PaperMinus()
  284. {
  285. char s[80];
  286.  
  287. paper = ((paper - 1) & 7);
  288. sprintf(s,"ink  %d\npaper %d\n",ink,paper);
  289. Help(s);
  290. }
  291.  
  292. help()
  293. {
  294. printf("this program opens a graphic window and makes the rastport public\n");
  295. printf("    -p n         specifies a number for the pipe.\n");
  296. printf("    -buf n       change size of vector buffer(used for hardcopy)\n");
  297. printf("            The Vectorbuffer may also be specified in Aux_Config\n");
  298. printf("            If you are very low on memory, you may specify a filename\n");
  299. printf("    -nohco       display only, no buffer for hardcopy\n");
  300. printf("    -paper n     specifies background colour\n");
  301. printf("    -ink n       specifies drawing colour\n");
  302. printf("    -left n      left edge\n");
  303. printf("    -top n       top edge\n");
  304. printf("    -xsize n     horizontal size\n");
  305. printf("    -ysize n     vertical size\n");
  306.    return(0);
  307. }
  308.  
  309. posita(x,y)
  310. int x,y;
  311. {
  312. int xx,yy;
  313.  
  314.    cx = x;
  315.    cy = y;
  316.  
  317.    if(!nohco) {
  318.      gfxbufin(0x1d);
  319.      cvtxy(x,y);
  320.    }
  321.  
  322.    xx=my_window->Width; xx=xx-1;
  323.    yy=my_window->Height; yy=yy-32;
  324.    xx=(xx*x)/_TEKXMAX;
  325.    yy=16+((yy*(_TEKYMAX-y))/_TEKYMAX);
  326.    Move(rp,xx,yy);
  327.    return(0);
  328. }
  329.  
  330. vectoa(x,y)
  331. int x,y;
  332. {
  333. int xx,yy;
  334.  
  335.    cx = x;
  336.    cy = y;
  337.  
  338.    if(!nohco) cvtxy(x,y);
  339.  
  340.    xx=my_window->Width; xx=xx-1;
  341.    yy=my_window->Height; yy=yy-32;
  342.    xx=(xx*x)/_TEKXMAX;
  343.    yy=16+((yy*(_TEKYMAX-y))/_TEKYMAX);
  344.    Draw(rp,xx,yy);
  345.    return(0);
  346. }
  347.  
  348. cls()
  349. {
  350. int xx,yy;
  351.  
  352.    xx=my_window->Width;
  353.    yy=my_window->Height;
  354.    SetAPen(rp,paper);
  355.    RectFill(rp,2,10,xx-3,yy-2);
  356.    SetAPen(rp,ink);
  357.    return(0);
  358. }
  359.  
  360. dperas()
  361. {
  362.    cls();
  363.    ptr=0;
  364.  
  365.    sizex = sizex_1;
  366.    sizey = sizey_1;
  367.    fontfile = font_1;
  368.    proptab = prop_1;
  369.    fontdesc = fontdesc_1;
  370.    fontheight = fontheight_1;
  371.    fontwidth = fontwidth_1;
  372.    firstchar = firstchar_1;
  373.    offschar = offschar_1;
  374.    prop_flag = 1;
  375.  
  376.    if(bufferonfile) {
  377.       close(fdbuffer);
  378.       fdbuffer = creat(bufferfile,0666);
  379.       close(fdbuffer);
  380.       fdbuffer = open(bufferfile,2,0666);
  381.    }
  382.    return(0);  
  383. }
  384.  
  385. getxy(x,y)
  386. int *x,*y;
  387. {
  388. int xx,yy,n;
  389. int newx, newy, oldx, oldy;
  390.  
  391.    xx=my_window->Width;
  392.    yy=my_window->Height;
  393.    oldx = xx / 2;
  394.    oldy = yy / 2;
  395.  
  396.    WindowToFront(my_window);
  397.    ActivateWindow(my_window);
  398.  
  399.    SetDrMd(rp, COMPLEMENT);
  400.    SetAPen(rp, 7);
  401.    Delay(5);
  402.  
  403.    Move(rp,oldx,0); Draw(rp,oldx,yy);
  404.    Move(rp,0,oldy); Draw(rp,xx,oldy);
  405.  
  406.    *x = -1; *y = -1;
  407.    while((*x < 0) || (*x > xx) || (*y < 0) || (*y > yy)) {
  408. /* wait for mouse button */
  409.       while((*((UBYTE*)0xbfe001))&64) {
  410.          Delay(5L);
  411.          Move(rp,oldx,0); Draw(rp,oldx,yy);
  412.          Move(rp,0,oldy); Draw(rp,xx,oldy);
  413.          newx=my_window->MouseX;
  414.          if(newx < 0) newx = 0;
  415.          if(newx > xx) newx = xx;
  416.          newy=my_window->MouseY;
  417.          if(newy < 0) newy = 0;
  418.          if(newy > yy) newy = yy;
  419.          Move(rp,newx,0); Draw(rp,newx,yy);
  420.          Move(rp,0,newy); Draw(rp,xx,newy);
  421.          oldx = newx; oldy = newy;
  422.       }
  423.       n=0;
  424.       *x=my_window->MouseX;
  425.       *y=my_window->MouseY;
  426.    }
  427.  
  428.    Move(rp,oldx,0); Draw(rp,oldx,yy);
  429.    Move(rp,0,oldy); Draw(rp,xx,oldy);
  430.  
  431.    *x = (( *x * _TEKXMAX) / xx);
  432.    *y = _TEKYMAX - (( *y * _TEKYMAX) / yy);
  433.  
  434.    SetDrMd(rp,JAM1);
  435.    SetAPen(rp, ink);
  436.    return(0);
  437. }
  438.  
  439. gfxtext(s)
  440. char s[];
  441. {
  442. int n;
  443. char c,z[256];
  444.  
  445.    c=s[0];
  446.    if(c==0x1b) {   /* chage text size */
  447.       c=s[1];
  448.       switch(c) {
  449.       case ';':                    /* smallest text size */
  450.          sizex = msizex / 2;
  451.          sizey = msizey / 2;
  452.          break;
  453.       case ':':                    /* small text size */
  454.          sizex = (msizex * 2) / 3;
  455.          sizey = (msizey * 2) / 3;
  456.          break;
  457.       case '9':
  458.          sizex = msizex;            /* normal text size */
  459.          sizey = msizey;
  460.          break;
  461.       case '8':
  462.          sizex = 2*msizex;          /* big text size */
  463.          sizey = 2*msizey;
  464.          break;
  465.       }
  466.       n=0;
  467.       while(c>0) {
  468.          c=s[n+2];
  469.          z[n++]=c;
  470.       }
  471.       plotstr(z);
  472.    } else {
  473.       plotstr(s);
  474.    }
  475.    return(0);
  476. }
  477.  
  478. textlen(s,l)
  479. char *s;
  480. int *l;
  481. {
  482. int c,i,n;
  483.  
  484.    i = 0; n = 0;
  485.    while(s[i] != 0) {
  486.       c = s[i++];
  487.       if(c == 92) {     /* meta character BACKSLASH ! */
  488.          i++;           /* ignore next character */
  489.          continue;
  490.       }
  491.       if(prop_flag == 1) {
  492.          n = n + sizex * proptab[c];
  493.       } else {
  494.          n = n + sizex * fontwidth;
  495.       }
  496.    }
  497.    n = n >> 1; /* ????????????????????! */
  498.    *l = n;
  499.    return(0);
  500. }
  501.  
  502. textheight(h)
  503. int *h;
  504. {
  505.    *h = sizey * fontheight;
  506.    return(0);
  507. }
  508.  
  509. setcolour(n)
  510. int n;
  511. {
  512. ink = n & 15;
  513. paper = n / 16;
  514. SetAPen(rp,ink);
  515.    return(0);
  516. }
  517.  
  518. rmtek()
  519. {
  520.    close_me = TRUE;
  521.    return(0);
  522. }
  523.  
  524. textparam(sx,sy,tx,ty,w,name)
  525. int sx,sy,tx,ty,w;
  526. char name[];
  527. {
  528.    if(sx>0) {sizex=sx; msizex=sx; sizex_1=sx;}
  529.    if(sy>0) {sizey=sy; msizey=sy; sizey_1=sx;}
  530.    if(tx>0) thickx=tx;
  531.    if(ty>0) thicky=ty;
  532.    if(name[0]!=0) {
  533.       readfont(name);
  534.       fontdesc_1 = fontdesc;
  535.       fontheight_1 = fontheight;
  536.       fontwidth_1 = fontwidth;
  537.       firstchar_1 = firstchar;
  538.       offschar_1 = offschar;
  539.    }
  540.    txtangle=w;
  541.    rotmat(w);
  542.    return(0);
  543. }
  544.  
  545. remove_tek()
  546. {
  547. int n;
  548.    n=1;
  549.    while(n>0) {
  550.       unlink(tekname);
  551.       n=open(tekname,0);
  552.       if(n>0) close(tekname);
  553.    }
  554.    return(0);
  555. }
  556.  
  557.  
  558. cvtxy(xx,yy)
  559. int xx,yy;
  560. {
  561. int i,xh,xl,yh,yl;
  562. int x4, y4;
  563.  
  564.    x4 = xx >> 2; y4 = yy >> 2;
  565.    yh = y4 >> 5; yl = y4 & 31;
  566.    xh = x4 >> 5; xl = x4 & 31;
  567.    i = (xx & 3) + 4 * (yy & 3);
  568.    gfxbufin(yh | 32);
  569.    gfxbufin(i | 96);
  570.    gfxbufin(yl | 96);
  571.    gfxbufin(xh | 32);
  572.    gfxbufin(xl | 64);
  573.    return(0);
  574. }
  575.  
  576. unsigned char getbuf(p)
  577. int p;
  578. {
  579.    if(bufferonfile) {
  580.       if(p == fdptr - 1) return(lastchar);
  581.       if(p == fdptr) {
  582.          read(fdbuffer,&lastchar,1);
  583.          fdptr++;
  584.          return(lastchar);
  585.       }
  586.       lseek(fdbuffer,p,0);
  587.       read(fdbuffer,&lastchar,1);
  588.       fdptr = p + 1;
  589.       return(lastchar);
  590.    } else {
  591.       return(buf[p]);
  592.    }
  593. }
  594.  
  595.  
  596. gettekxy(p,x,y)
  597. int *p,*x,*y;
  598. {
  599. int xx,yy,i,l,h,ptr,k,z1,z2;
  600. char c;
  601.  
  602.    z1 = -1; z2 = -1;
  603.    xx = -1 ; yy = -1;
  604.    ptr = *p;
  605.    for(k=1;k<10;k++) {
  606.       c=getbuf(ptr++);
  607.       i=(c & 0xe0);
  608.       switch(i) {
  609.       case 32:
  610.          h = (c & 31);
  611.          break;
  612.       case 64:
  613.          l = (c & 31);
  614.          xx = (h*32 + l) << 2;
  615.          if(z2 != -1) xx = xx | z2;
  616.          break;
  617.       case 96:
  618.          l = (c & 31);
  619.          if(z1 != -1) {
  620.             yy = h*32 + l;
  621.             yy = (yy << 2) | (z1 >> 2);
  622.             z2 = z1 & 3;
  623.          } else {
  624.             yy = (h*32 + l) << 2;
  625.             z1 = l & 15;
  626.          }
  627.          break;
  628.       }
  629.       if((xx != -1) && (yy != -1)) break;
  630.    }
  631.    *x = xx; *y = yy; *p = ptr;
  632.    return(0);
  633. }
  634.  
  635. gfxbufin(c)
  636. char c;
  637. {
  638.    if(bufferonfile) {
  639.       write(fdbuffer,&c,1); 
  640.       ptr++; fdptr++;
  641.    } else {
  642.       if(ptr<bufsiz) buf[ptr++]=c;
  643.    }
  644.    return(0);
  645. }
  646.  
  647.  
  648. /* *********************************************************************
  649.  
  650.                   Vector font utilities
  651.  
  652.    ********************************************************************* */
  653.  
  654. /* ------------------------------------------------------------------
  655.       Read Vector font from file (as defined in Aux_Config)
  656.    ------------------------------------------------------------------ */
  657. readfont(name)
  658. char *name;
  659. {
  660. int n,i,m,xx,max;
  661. int yy,base;
  662. unsigned char *ptr;
  663. int fd;
  664.  
  665.    fd = open(name,0);
  666.    read(fd,fontfile,16384);
  667.    close(fd);
  668.    n = 0;
  669.    while(fontfile[n] != 0x1a) if(n++ > 32767) break;
  670.    i = fontfile[n+1] + 256 * fontfile[n + 2];
  671.    ptr = &fontfile[i];
  672.    if(ptr[0] != 0x2b) {
  673.       printf("no font file\n");
  674.       exit(0);
  675.    }
  676.    offschar = i + ptr[5] + 256 * ptr[6];
  677.    firstchar = ptr[4];
  678.    fontheight = ptr[8] + ptr[11];
  679.    fontdesc = &ptr[18];
  680.    base = 0;
  681.    m = 128 - firstchar;
  682.    for(n = 0; n < m; n++) {         /* check fontwidth, generate prop table */
  683.       i = offschar + fontdesc[2 * n] + 256 * fontdesc[2 * n + 1];
  684.       ptr = &fontfile[i];
  685.       i = 0; max = 0;
  686.       while((ptr[i] & 0x80) != 0) {
  687.          xx = ptr[i++]; yy = ptr[i++];
  688.          xx = xx & 0x7f; yy = yy & 0x7f;
  689.          if(xx > max) max = xx;
  690.          if((yy & 0x40) != 0) {
  691.             yy = (yy & 0x3f) - 0x3f;
  692.             if(yy < base) base = yy;
  693.          }
  694.       }
  695.       max = max + 2;
  696.       proptab[n + firstchar + 1] = max;
  697.    }
  698.    base = -1 * base; fontheight = 0;
  699.    for(n = 0; n < m; n++) {         /* move baseline */
  700.       i = offschar + fontdesc[2 * n] + 256 * fontdesc[2 * n + 1];
  701.       ptr = &fontfile[i];
  702.       i = 0; max = 0;
  703.       while((ptr[i] & 0x80) != 0) {
  704.          i++; yy = ptr[i];
  705.          xx = yy; yy = yy & 0x7f;
  706.          if((yy & 0x40) != 0) yy = (yy & 0x3f) - 0x3f;
  707.          yy = yy + base;
  708.          if(yy > fontheight) fontheight = yy;
  709.          yy = yy | (xx & 0x80);
  710.          ptr[i++] = yy;
  711.       }
  712.    }
  713.    fontheight = fontheight / 2;
  714.    proptab[' '] = proptab['i'];
  715.    fontwidth = proptab['W'];
  716.    return(0);
  717. }
  718.  
  719.  
  720. /* ------------------------------------------------------------------
  721.       Rotation
  722.    ------------------------------------------------------------------ */
  723. rotate(xx,yy)
  724. int *xx,*yy;
  725. {
  726. int rx,ry,x,y;
  727.  
  728.    x = *xx ; y = *yy;
  729.    rx = ((x * cosphi) >> 19) - (( y * sinphi) >> 19);
  730.    ry = (( y * cosphi) >> 19) + ((x * sinphi) >> 19);
  731.    *xx = rx ; *yy = ry;
  732.    return(0);
  733. }
  734. /* ------------------------------------------------------------------
  735.       plot one single character
  736.    ------------------------------------------------------------------ */
  737. plotchar(c)
  738. int c;
  739. {
  740. int xx,yy;
  741. int ix,iy;
  742. int i,n;
  743. unsigned char *ptr;
  744.  
  745.    ix = cx; iy = cy;
  746.  
  747.    posita(ix,iy);
  748.  
  749.    n = c - firstchar - 1;
  750.    i = offschar + fontdesc[2 * n] + 256 * fontdesc[2 * n + 1];
  751.    ptr = &fontfile[i];
  752.    i = 0;
  753.    while((ptr[i] & 0x80) != 0) {
  754.       xx = ptr[i++]; yy = ptr[i++];
  755.       n = 0;
  756.       if((yy & 0x80) != 0) n = 1;
  757.       xx = xx & 0x7f; yy = yy & 0x7f;
  758.       xx = xx * sizex;
  759.       yy = yy * sizey;
  760.       rotate(&xx,&yy);
  761.       if(n == 1) {
  762.          vectoa(ix + xx, iy + yy);
  763.       } else {
  764.          posita(ix + xx, iy + yy);
  765.       }
  766.    }
  767.    xx = sizex * fontwidth; yy = 0;
  768.    if(prop_flag == 1) xx = sizex * proptab[c];
  769.    rotate(&xx ,&yy);
  770.    cx = ix + xx;
  771.    cy = iy + yy;
  772.    return(0);
  773. }
  774.  
  775. /* ------------------------------------------------------------------
  776.       plot string
  777.    ------------------------------------------------------------------ */
  778. plotstr(s)
  779. char s[];
  780. {
  781. int i,mx,my,tx,ty;
  782. int msizex, msizey, mprop;
  783. unsigned char c;
  784.  
  785.    cy = cy - 4;
  786.    msizex = sizex;
  787.    msizey = sizey;
  788.    mprop = prop_flag;
  789.    mx = cx ; my = cy;
  790.    for(tx=0;tx<thickx;tx++) {
  791.       for(ty=0;ty<thicky;ty++) {
  792.          cx = mx + tx;
  793.          cy = my + ty;
  794.          i=0;
  795.          baseline = 0;
  796.          sizex = msizex;
  797.          sizey = msizey;
  798.          fontfile = font_1;
  799.          proptab = prop_1;
  800.          fontdesc = fontdesc_1;
  801.          fontheight = fontheight_1;
  802.          fontwidth = fontwidth_1;
  803.          firstchar = firstchar_1;
  804.          offschar = offschar_1;
  805.          prop_flag = mprop;
  806.  
  807.          while(1==1) {
  808.             c = s[i++];
  809.             if(c==0) break;
  810.             if(c==92) {   /* meta character BACKSLASH ! */
  811.                c = s[i++];
  812.                switch(c) {
  813.                   case 'u':   /* superscript */
  814.                      baseline = baseline +1;
  815.                      if(baseline > 0) {
  816.                         cy = cy + ((sizey * fontheight) >> 1);
  817.                         sizex = (sizex << 2) / 5;
  818.                         sizey = (sizey << 2) / 5;
  819.                         continue;
  820.                      }
  821.                      if(baseline == 0) {
  822.                         cy = my + ty;
  823.                         sizex = msizex;
  824.                         sizey = msizey;
  825.                         continue;
  826.                      }
  827.                      if(baseline < 0) {
  828.                         cy = cy + ((sizey * fontheight) >> 1);
  829.                         sizex = (5 * sizex) >> 2;
  830.                         sizey = (5 * sizey) >> 2;
  831.                         continue;
  832.                      }
  833.                   case 'd':   /* subscript */
  834.                      baseline = baseline -1;
  835.                      if(baseline < 0) {
  836.                         cy = cy - ((sizey * fontheight) >> 1);
  837.                         sizex = (sizex << 2) / 5;
  838.                         sizey = (sizey << 2) / 5;
  839.                         continue;
  840.                      }
  841.                      if(baseline == 0) {
  842.                         cy = my + ty;
  843.                         sizex = msizex;
  844.                         sizey = msizey;
  845.                         continue;
  846.                      }
  847.                      if(baseline > 0) {
  848.                         cy = cy - ((sizey * fontheight) >> 1);
  849.                         sizex = (5 * sizex) >> 2;
  850.                         sizey = (5 * sizey) >> 2;
  851.                         continue;
  852.                      }
  853.                   case '1':   /* primary font */
  854.                         fontfile = font_1;
  855.                         proptab = prop_1;
  856.                         fontdesc = fontdesc_1;
  857.                         fontheight = fontheight_1;
  858.                         fontwidth = fontwidth_1;
  859.                         firstchar = firstchar_1;
  860.                         offschar = offschar_1;
  861.                         continue;
  862.                   case '2':   /* secondary font */
  863.                         fontfile = font_2;
  864.                         proptab = prop_2;
  865.                         fontdesc = fontdesc_2;
  866.                         fontheight = fontheight_2;
  867.                         fontwidth = fontwidth_2;
  868.                         firstchar = firstchar_2;
  869.                         offschar = offschar_2;
  870.                         continue;
  871.                   case '0':   /* proportional off */
  872.                         prop_flag = 0;
  873.                         continue;
  874.                   case 'b':   /* backspace */
  875.                         cx = (cx - (sizex * fontwidth) >> 1);
  876.                         continue;
  877.                }
  878.             }
  879.             plotchar(c);
  880.          }
  881.       }
  882.    }
  883.    baseline = 0;
  884.    sizex = msizex;
  885.    sizey = msizey;
  886.    fontfile = font_1;
  887.    proptab = prop_1;
  888.    fontdesc = fontdesc_1;
  889.    fontheight = fontheight_1;
  890.    fontwidth = fontwidth_1;
  891.    firstchar = firstchar_1;
  892.    offschar = offschar_1;
  893.    prop_flag = mprop;
  894.    return(0);
  895. }
  896.  
  897. /* ----------------------------------------------------
  898.        Calculate rotation matrix
  899.    ---------------------------------------------------- */
  900.  
  901. rotmat(n)
  902. int n;      /* Angle in degree */
  903. {
  904. float phi,cphi,sphi;
  905.  
  906.    phi = ((float) n) * 3.1415926 / 180.0;
  907.    cphi = 262144 * cos( phi );
  908.    sphi = 262144 * sin( phi );
  909.    sinphi = (int) sphi;
  910.    cosphi = (int) cphi;
  911.    return(0);
  912. }
  913.  
  914. xtoi(s)
  915. char  s[];
  916. {  int i,m,l,e;
  917.    char c;
  918.  
  919.    l=strlen(s); e=0;
  920.    for( i = 0 ; i < l ; i++ ) {
  921.       c = s[i] ; c = toupper(c) ;
  922.       m = c - '0' ;
  923.       if( m > 9 ) m = m - 7 ;
  924.       e = 16 * e + m ;
  925.    } ;
  926.    return(e) ;
  927. }
  928.  
  929. /* ****************************************************************
  930.  
  931.                   HARDCOPY and related functions
  932.  
  933.    **************************************************************** */
  934. void hco_()
  935. {
  936. hco(defaultprinter);
  937. }
  938.  
  939. hco(str)
  940. char str[];
  941. {
  942. unsigned char *bitplane;
  943. char z[80];
  944. int n;
  945.  
  946.    pribufsiz=8190;
  947.    pribufptr=0;
  948.    printerbuf=(unsigned char *)calloc(pribufsiz+2,sizeof(char));
  949.    if(buf==NULL) {
  950.       fprintf(stderr,"could not allocate printer buffer %d bytes\n",pribufsiz);
  951.       return(0);
  952.    }
  953.  
  954.  
  955.    strcpy(z,defaultprinter);
  956.    if(str[0]!=0) strcpy(z,str);
  957.    fdprinter=auxopen(z);
  958.    strcpy(systemcommand,auxparams[4]);
  959.  
  960.    n=5;
  961.    prt_passes=atoi(auxparams[n]); n=n+1;
  962.    prt_lsbmsb=atoi(auxparams[n]); n=n+1;
  963.    x_prt_siz=atoi(auxparams[n]); n=n+1;
  964.    y_prt_siz=atoi(auxparams[n]); n=n+1;
  965.  
  966.    if(prt_lsbmsb == 10) {
  967.       save_ps(auxparams[0],x_prt_siz,y_prt_siz);
  968.       system2(systemcommand);
  969.       return(0);
  970.    }
  971.    if(prt_lsbmsb == 11) {
  972.       save_hpgl(auxparams[0],x_prt_siz,y_prt_siz);
  973.       system2(systemcommand);
  974.       return(0);
  975.    }
  976.  
  977.    cvtbs(prt_init,auxparams[n]); n=n+1;
  978.    cvtbs(prt_gra,auxparams[n]); n=n+1;
  979.    cvtbs(prt_halflf,auxparams[n]); n=n+1;
  980.    cvtbs(prt_lf,auxparams[n]); n=n+1;
  981.    cvtbs(prt_fin,auxparams[n]);
  982.  
  983.    if(fdprinter <= 0) {
  984.       fdprinter = creat(auxparams[0],0666);
  985.       if(fdprinter <= 0) return(0);
  986.    }
  987.  
  988.    n = (x_prt_siz * y_prt_siz) / 8;     /* length of buffer needed */
  989.    n = n + 1024;                        /* to go shure */
  990.    bitplane=(unsigned char *)calloc(n,sizeof(char));
  991.    if(buf==NULL) {
  992.       fprintf(stderr,"could not allocate shadow memory %d bytes\n",n);
  993.       free(printerbuf);
  994.       close(fdprinter);
  995.       return(0);
  996.    }
  997.    redrawhco(bitplane);
  998.    printout(bitplane);
  999.    close(fdprinter);
  1000.    free(bitplane); free(printerbuf); 
  1001.    system2(systemcommand);
  1002.    return(0);
  1003. }
  1004.  
  1005. strtoprt(s)
  1006. unsigned char s[];
  1007. {
  1008. int n;
  1009. unsigned char c;
  1010.  
  1011.    n=0;
  1012.    while(1==1) {
  1013.       c=s[n++];
  1014.       if(c==0) break;
  1015.       pribufin(c);
  1016.    }
  1017.    return(0);
  1018. }
  1019.  
  1020. pribufin(c)
  1021. unsigned char c;
  1022. {
  1023.    if(pribufptr>=pribufsiz) pribufflush();
  1024.    printerbuf[pribufptr++]=c;
  1025.    return(0);
  1026. }
  1027.  
  1028. pribufflush()
  1029. {
  1030. write(fdprinter,printerbuf,pribufptr); 
  1031. pribufptr=0;
  1032.    return(0);
  1033. }
  1034.  
  1035. redrawhco(bitplane)
  1036. unsigned char *bitplane;
  1037. {
  1038. int x,y,p;
  1039. char c;
  1040.  
  1041.    p=0;
  1042.    while(getbuf(p)!=0x1d) p = p + 1;   /* find posita first */
  1043.  
  1044.    while(p<ptr) {                      /* scan vector buffer */
  1045.  
  1046.       c=getbuf(p);
  1047.       if(c==0x1d) {
  1048.          p=p+1;
  1049.          gettekxy(&p,&x,&y);
  1050.          if((x>_TEKXMAX) | (y>_TEKYMAX)) {
  1051.             while(getbuf(p)!=0x1d) p=p+1;
  1052.             continue;
  1053.          }
  1054.          draw_to(bitplane,FALSE,x,y);
  1055.          continue;
  1056.       }
  1057.       if(c>31) {
  1058.          gettekxy(&p,&x,&y);
  1059.          if((x>_TEKXMAX) | (y>_TEKYMAX)) {
  1060.             while(getbuf(p)!=0x1d) p=p+1;
  1061.             continue;
  1062.          }
  1063.          draw_to(bitplane,TRUE,x,y);
  1064.          continue;
  1065.       }
  1066.       while(getbuf(p)!=0x1d) p=p+1;          /* find next posita */
  1067.    }
  1068.    return(0);
  1069. }
  1070.  
  1071.  
  1072. redraw2()
  1073. {
  1074. int x,y,p;
  1075. int sto_nohco;
  1076. int sto_ptr;
  1077. char c;
  1078.  
  1079.    if(nohco) return(0);
  1080.  
  1081.    Forbid();  /* taskswitching */
  1082.  
  1083.    sto_nohco = nohco;
  1084.    sto_ptr = ptr;
  1085.    nohco = TRUE;
  1086.    
  1087.    cls();
  1088.    ptr = sto_ptr;
  1089.  
  1090.    p=0;
  1091.    while(getbuf(p)!=0x1d) p = p + 1;   /* find posita first */
  1092.  
  1093.    while(p<ptr) {                      /* scan vector buffer */
  1094.  
  1095.       c=getbuf(p);
  1096.       if(c==0x1d) {
  1097.          p=p+1;
  1098.          gettekxy(&p,&x,&y);
  1099.          if((x>_TEKXMAX) | (y>_TEKYMAX)) {
  1100.             while(getbuf(p)!=0x1d) p=p+1;
  1101.             continue;
  1102.          }
  1103.          posita(x,y);
  1104.          continue;
  1105.       }
  1106.       if(c>31) {
  1107.          gettekxy(&p,&x,&y);
  1108.          if((x>_TEKXMAX) | (y>_TEKYMAX)) {
  1109.             while(getbuf(p)!=0x1d) p=p+1;
  1110.             continue;
  1111.          }
  1112.          vectoa(x,y);
  1113.          continue;
  1114.       }
  1115.       while(getbuf(p)!=0x1d) p=p+1;          /* find next posita */
  1116.    }
  1117.    nohco = sto_nohco;
  1118.    ptr = sto_ptr;
  1119.    Permit();      /* Taskswitching */
  1120.    return(0);
  1121. }
  1122.       
  1123. /* --------------------------------------------------------------- */
  1124. /*       Draw a line                                               */
  1125. /* --------------------------------------------------------------- */
  1126. draw_to(bitplane,pen_down,x,y)
  1127. unsigned char *bitplane;
  1128. int pen_down,x,y;
  1129. {
  1130.    int   xinc,yinc,
  1131.          xcount,ycount,
  1132.          fx,fy;
  1133.  
  1134.  
  1135.    x = (x_prt_siz * x) / _TEKXMAX ;
  1136.    y = (y_prt_siz * y) / _TEKYMAX ; y=y_prt_siz-y;
  1137.  
  1138.    if(pen_down == TRUE) {
  1139.       xinc=liabs(x-aposx);
  1140.       yinc=liabs(y-aposy);
  1141.       fx=lisgn(x-aposx);
  1142.       fy=lisgn(y-aposy);
  1143.       xcount=xinc;
  1144.       ycount=yinc;
  1145.       do {
  1146.          set_pixel(bitplane,aposx,aposy);
  1147.          if(hcothick) {
  1148.             set_pixel(bitplane,aposx+1,aposy);
  1149.             set_pixel(bitplane,aposx,aposy+1);
  1150.             set_pixel(bitplane,aposx+1,aposy+1);
  1151.          }
  1152.          if(xcount>ycount) {
  1153.             ycount=ycount+yinc ; aposx=aposx+fx;
  1154.          } else {
  1155.             xcount=xcount+xinc ; aposy=aposy+fy;
  1156.          }
  1157.       } while((liabs(x-aposx)>=1) || (liabs(y-aposy)>=1));
  1158.    }
  1159.    aposx=x; aposy=y;
  1160.    return(0);
  1161. }
  1162.  
  1163. liabs(x)
  1164. int x;
  1165. {
  1166.    if(x>=0) return(x);
  1167.    return(-x);
  1168. }
  1169.  
  1170. lisgn(x)
  1171. int  x;
  1172. {
  1173.    if(x>0) return(1L);
  1174.    if(x<0) return(-1L);
  1175.    return(0L);
  1176. }
  1177.  
  1178. /* --------------------------------------------------------------- */
  1179. /*       plot one pixel                                            */
  1180. /* --------------------------------------------------------------- */
  1181. set_pixel(bitplane,x,y)
  1182. unsigned char *bitplane;
  1183. int x,y;
  1184. {
  1185.    register int  byte,bit,n;
  1186.  
  1187.  
  1188.  
  1189.    byte=y*(x_prt_siz>>3)+(x>>3);
  1190.    bit= x & 7; n=128>>bit;
  1191.    bitplane[byte] |= n;
  1192.    return(0);
  1193. }
  1194.  
  1195. /* --------------------------------------------------------------- */
  1196. /*       get one pixel from shadow memory                          */
  1197. /* --------------------------------------------------------------- */
  1198. get_pixel(bitplane,x,y)
  1199. unsigned char *bitplane;
  1200. int x,y;
  1201. {
  1202.    register int   byte,bit;
  1203.    register unsigned char c;
  1204.  
  1205.    byte=y*(x_prt_siz>>3)+(x>>3);
  1206.    bit=7-(x & 7);
  1207.    c=((bitplane[byte])>>bit) & 1;
  1208.    return((int)c);
  1209. }
  1210.  
  1211. /* ----------------------------------------------------------- */
  1212. /*       get one byte ready to print from shadow memory        */
  1213. /* ----------------------------------------------------------- */
  1214. get_prtbyte(bitplane,x,y)
  1215. unsigned char *bitplane;
  1216. int x,y;
  1217. {
  1218.    int    i,m;
  1219.    long   n;
  1220.  
  1221.    m=0; n=0; x=x_prt_siz-x;
  1222.    for(i=1; i<=8; ++i) {
  1223.       n=n+prt_passes;
  1224.       m=2*m+get_pixel(bitplane,x-n,y);
  1225.    }
  1226.    return(m);
  1227. }
  1228.  
  1229. /* ----------------------------------------------------------- */
  1230. /*       make hardcopy of shadow memory                        */
  1231. /* ----------------------------------------------------------- */
  1232. printout(bitplane)
  1233. unsigned char *bitplane;
  1234. {
  1235.    long  x,y,i;
  1236.    unsigned char c;
  1237.  
  1238.  
  1239.    strtoprt(prt_init); pribufflush();
  1240.  
  1241.    if(prt_lsbmsb == 2) {              /* trap HP raster row graphics */
  1242.       hp_rasterrow(bitplane);
  1243.       return(0);
  1244.    }
  1245.  
  1246.    if(prt_lsbmsb == 3) {              /* trap HP DeskJet 500 */
  1247.       hp_deskjet(bitplane);
  1248.       return(0);
  1249.    }
  1250.  
  1251.    /* it follows the epson style printout routine */
  1252.  
  1253.    x=0;
  1254.    while(x<x_prt_siz) {
  1255.       y=0;
  1256.       for(i=1; i<=prt_passes; ++i) {
  1257.          prt_gra_out(y_prt_siz);
  1258.          while(y<y_prt_siz) {
  1259.             c=get_prtbyte(bitplane,x,y++);
  1260.             pribufin(c);
  1261.          }
  1262.          strtoprt(prt_halflf); 
  1263.          y=0; ++x;
  1264.       }
  1265.       strtoprt(prt_lf); pribufflush();
  1266.       x=x+7*prt_passes;
  1267.    }
  1268.  
  1269.    strtoprt(prt_fin); pribufflush();
  1270.    return(0);
  1271. }
  1272.  
  1273. /* -----------------------------
  1274.    HP mode raster row graphics 
  1275.    ----------------------------- */
  1276.  
  1277. hp_rasterrow(bitplane)
  1278. unsigned char *bitplane;
  1279. {
  1280.    int  n,x,m,i;
  1281.    unsigned char c;
  1282.  
  1283.  
  1284.    m = x_prt_siz / 8;
  1285.    n = (y_prt_siz -1) * m;
  1286.    x=0;
  1287.    while(x<n) {
  1288.       strtoprt(prt_gra);
  1289.       for(i=0; i<m; i++) {
  1290.          c=bitplane[x++];
  1291.          pribufin(c);
  1292.       }
  1293.       pribufflush();
  1294.    }
  1295.    strtoprt(prt_fin); pribufflush();
  1296.    return(0);
  1297. }
  1298.  
  1299. /* -----------------------------
  1300.       HP DeskJet mode  
  1301.    ----------------------------- */
  1302.  
  1303. hp_deskjet(bitplane)
  1304. unsigned char *bitplane;
  1305. {
  1306. int  x,y,i;
  1307. unsigned char c;
  1308.  
  1309.    x=0;
  1310.    while(x<x_prt_siz) {
  1311.       y = y_prt_siz - 1;
  1312.       strtoprt(prt_gra);
  1313.       while(y >= 0) {
  1314.          c = 0;
  1315.          for(i = 0; i < 8; i++) {
  1316.             c = (2 * c) + get_pixel(bitplane,x,y--);
  1317.          }
  1318.          pribufin(c);
  1319.       }
  1320.       pribufflush();
  1321.       x = x + 1;
  1322.    }
  1323.    strtoprt(prt_fin); pribufflush();
  1324.    return(0);
  1325. }
  1326.  
  1327. /* ------------------------------------------------------------ */
  1328. /* some printer dependend routines:                             */
  1329. /* ------------------------------------------------------------ */
  1330.  
  1331. prt_gra_out(x)
  1332. int  x;
  1333. {
  1334. unsigned char c1,c2;
  1335.  
  1336.    c1=x/256; c2=x-256*c1;
  1337.    strtoprt(prt_gra);
  1338.    if(prt_lsbmsb==1) {
  1339.       pribufin(c2);
  1340.       pribufin(c1);
  1341.    } else {
  1342.       pribufin(c1);
  1343.       pribufin(c2);
  1344.    }
  1345.    return(0);
  1346. }
  1347.  
  1348.  
  1349. /* convert string which includes the backslash */
  1350.  
  1351. cvtbs(s2,s1)
  1352. char s2[] , s1[];
  1353. {  int   l , i2 , i1 ;
  1354.    char  c , cc , s[8];
  1355.  
  1356.    l=strlen(s1); i2=0 ;
  1357.    for (i1 = 0; i1 <= l; i1++) {
  1358.       c=s1[i1];
  1359.       if(c == ' ') continue;
  1360.       if(c == '\\') {
  1361.          cc=s1[++i1];
  1362.          switch(cc) {
  1363.          case 'n':
  1364.             c='\n' ;  break ;
  1365.          case 'f':
  1366.             c='\f' ;  break ;
  1367.          case 't':
  1368.             c='\t' ;  break ;
  1369.          case 'b':
  1370.             c='\b' ;  break ;
  1371.          case 'x': /* hexadecimal conversion */
  1372.             s[0]=s1[++i1] ; s[1]=s1[++i1] ; s[2]=0 ;
  1373.             c=xtoi(s) ; break ;
  1374.          default:
  1375.             ; } ;
  1376.          } ;
  1377.       s2[i2++]=c ; }
  1378.    s2[i2]=0 ;
  1379.    return(i2-1);
  1380. }
  1381.  
  1382. /* ---------------------------------------------------------------
  1383.    Special actions: set text parameters and make hardcopy
  1384.    --------------------------------------------------------------- */
  1385. do_special(param)
  1386. char param[];
  1387. {
  1388. int i,n,sx,sy,tx,ty,w;
  1389. char s[80];
  1390.  
  1391.    sscanf(param,"%d",&n);
  1392.  
  1393.    hcothick = FALSE;
  1394.    if(n > 127) {
  1395.       hcothick = TRUE;
  1396.       n = n && 127;
  1397.    }
  1398.  
  1399.    strcpy(s,"");
  1400.    switch(n) {
  1401.    case 0 :                                     /* textparam */
  1402.       sscanf(param,"%d %d %d %d %d %d %s",&i,&sx,&sy,&tx,&ty,&w,s);
  1403.       if(sx>0) {sizex=sx; msizex=sx;}
  1404.       if(sy>0) {sizey=sy; msizey=sy;}
  1405.       if(tx>0) thickx=tx;
  1406.       if(ty>0) thicky=ty;
  1407.       if(s[0]!=0) readfont(s);
  1408.       txtangle=w;
  1409.       rotmat(w);
  1410.       break;
  1411.    case 1 :                                     /* hco */
  1412.       sscanf(param,"%d %s",&i,s);
  1413.       hco(s);
  1414.       break;
  1415.    case 2 :
  1416.       sscanf(param,"%d %s",&i,s);               /* print HP-GL */
  1417.       save_hpgl(s,0,0);
  1418.       break;
  1419.    case 3 :
  1420.       sscanf(param,"%d %s",&i,s);               /* print PostScript */
  1421.       save_ps(s,0,0);
  1422.       break;
  1423.    case 4 :
  1424.       sscanf(param,"%d %s",&i,s);               /* print Amiga IFF */
  1425.       save_iff(s);
  1426.       break;
  1427.    case 5 :
  1428.       sscanf(param,"%d %s",&i,s);               /* print Aegis Draw */
  1429.       save_adraw(s,0,0);
  1430.       break;
  1431.    case 6 :
  1432.       sscanf(param,"%d %s",&i,s);               /* print DXF */
  1433.       save_dxf(s,0,0);
  1434.       break;
  1435.    }
  1436.    return(0);
  1437. }
  1438.  
  1439. teknewopen()
  1440. {
  1441. int   n,_tek4014;
  1442. char  c,s[80],z[80];
  1443.  
  1444.    strcpy(s,"gfx");
  1445.    c=_tekpipe+48;
  1446.    z[0]=c; z[1]=0;
  1447.    strcat(s,z);
  1448.    _tek4014=auxopen(s); /* _tekpipe is defined in spec.h */
  1449.    if(_tek4014 > 0) close(_tek4014);
  1450.    strcpy(tekname,auxparams[0]);
  1451.    strcpy(primaryfont,auxparams[1]);
  1452.    sizex_1 = atoi(auxparams[2]);
  1453.    sizey_1 = atoi(auxparams[3]);
  1454.    sizex = sizex_1; sizey = sizey_1;
  1455.    msizex = sizex; msizey = sizey;
  1456.    strcpy(secondaryfont,auxparams[4]);
  1457.    sizex_2 = atoi(auxparams[5]);
  1458.    sizey_2 = atoi(auxparams[6]);
  1459.    strcpy(bufferfile,auxparams[7]);
  1460.    if(bufferfile[0] > '9') {
  1461.       bufferonfile = TRUE;
  1462.    } else {
  1463.       bufferonfile = FALSE;
  1464.       bufsiz = atoi(bufferfile);
  1465.    }
  1466.    strcpy(defaultprinter,auxparams[8]);
  1467.    _tek4014=creat(tekname,0777);
  1468.    if(_tek4014 <= 0) {
  1469.       fprintf(stderr,"failed to open graphic file\n");
  1470. /*      close_gfx(my_window); */
  1471.       exit(-1);
  1472.    }
  1473.       n = (int) *posita; write(_tek4014,&n,4);
  1474.       n = (int) *vectoa; write(_tek4014,&n,4);
  1475.       n = (int) *dperas; write(_tek4014,&n,4);
  1476.       n = (int) *gfxtext; write(_tek4014,&n,4);
  1477.       n = (int) *getxy; write(_tek4014,&n,4);
  1478.       n = (int) *setcolour; write(_tek4014,&n,4);
  1479.       n = (int) *rmtek; write(_tek4014,&n,4);
  1480.       n = (int) *textparam; write(_tek4014,&n,4);
  1481.       n = (int) *hco; write(_tek4014,&n,4);
  1482.       n = (int) *do_special; write(_tek4014,&n,4);
  1483.       n = (int) *textlen; write(_tek4014,&n,4);
  1484.       n = (int) *textheight; write(_tek4014,&n,4);
  1485.    close(_tek4014);
  1486.    return(0);
  1487. }
  1488.  
  1489.  
  1490. /* ---------------------------------------------------------------
  1491.  
  1492.       Diverse Vektororientierte Ausgabe formate:
  1493.  
  1494.       HP-GL , DXF, PostScript
  1495.  
  1496.    --------------------------------------------------------------- */
  1497.  
  1498. vector_out(output,x_faktor,y_faktor,x_ofs,y_ofs,move_to,draw_to,bugfix)
  1499.  FILE *output;
  1500.  char move_to[], draw_to[];
  1501.  float x_faktor, y_faktor;
  1502.  float x_ofs, y_ofs;
  1503.  int bugfix;
  1504. {
  1505. int x,y,p;
  1506. float fx,fy,ofx,ofy;
  1507. char c;
  1508. int linecount;          /* This variable is introduced to work around a "bug" in most
  1509.                            PostScript interpreters, which have only restricted memory
  1510.                            capacities                                                 */
  1511.  
  1512.    p=0; linecount = 0;
  1513.    while(getbuf(p)!=0x1d) p = p + 1;   /* find posita first */
  1514.  
  1515.    while(p<ptr) {                      /* scan vector buffer */
  1516.  
  1517.       c=getbuf(p);
  1518.       if(c==0x1d) {
  1519.          p=p+1;
  1520.          gettekxy(&p,&x,&y);
  1521.          if((x < 0) | (y < 0) | (x>_TEKXMAX) | (y>_TEKYMAX)) {
  1522.             while(getbuf(p)!=0x1d) p=p+1;
  1523.             continue;
  1524.          }
  1525.          ofx = (x_faktor * x) + x_ofs;
  1526.          ofy = (y_faktor * y) + y_ofs;
  1527.          if(bugfix != 2) fprintf(output,move_to,ofx,ofy);
  1528.          linecount = 0;
  1529.          continue;
  1530.       }
  1531.       if(c>31) {
  1532.          gettekxy(&p,&x,&y);
  1533.          if((x < 0) | (y < 0) | (x>_TEKXMAX) | (y>_TEKYMAX)) {
  1534.             while(getbuf(p)!=0x1d) p=p+1;
  1535.             continue;
  1536.          }
  1537.          fx = (x_faktor * x) + x_ofs;
  1538.          fy = (y_faktor * y) + y_ofs;
  1539.          if(bugfix == 2) {   /* DXF special treatment */
  1540.             fprintf(output,move_to,ofx,ofy);
  1541.             ofx = fx; ofy = fy;
  1542.          }
  1543.          fprintf(output,draw_to,fx,fy);
  1544.          if((linecount++ > 20) && (bugfix == 1)) {   /* work around PostScript "bug" */
  1545.             linecount = 0;
  1546.             fprintf(output,move_to,fx,fy);
  1547.          }
  1548.          continue;
  1549.       }
  1550.       while(getbuf(p)!=0x1d) p = p + 1;   /* find next posita */
  1551.    }
  1552.    return(0);
  1553. }
  1554.  
  1555. save_hpgl_()
  1556. {
  1557. char s[80];
  1558.  
  1559.    FileSelect("Select HP-GL file",s);
  1560.    save_hpgl(s,0,0);
  1561.    return(0);
  1562. }
  1563.  
  1564. save_hpgl(s,xsize,ysize)
  1565. char *s;
  1566. int xsize, ysize;
  1567. {
  1568. FILE *output;
  1569. float x_factor, y_factor;
  1570.  
  1571.    x_factor = (float) xsize / 100.0; if(xsize == 0) x_factor = 2.25;
  1572.    y_factor = (float) ysize / 100.0; if(ysize == 0) y_factor = 2.25;
  1573.  
  1574.    output = fopen(s,"w");
  1575.    fprintf(output,"IN;\nLT;\n1;\n");
  1576.    vector_out(output,x_factor,y_factor,0.0,0.0,
  1577.       "PU;PA %5.0f,%5.0f;\n",
  1578.       "PD;PA %5.0f,%5.0f;\n",0);
  1579.    fprintf(output,"PU;SP;IP;SC;PA 0,7650\n");
  1580.    fclose(output);
  1581.    return(0);
  1582. }
  1583.  
  1584. save_adraw_()
  1585. {
  1586. char s[80];
  1587.  
  1588.    FileSelect("Select adraw file",s);
  1589.    save_adraw(s,0,0);
  1590.    return(0);
  1591. }
  1592.  
  1593. save_adraw(s,xsize,ysize)
  1594. char *s;
  1595. int xsize, ysize;
  1596. {
  1597. FILE *output;
  1598. float x_factor, y_factor;
  1599.  
  1600.    x_factor = 0.24;
  1601.    y_factor = 0.24;
  1602.  
  1603.    output = fopen(s,"w");
  1604.    fprintf(output,"81086 0.00000 0.00000 1000.0 1000.0 0 1.00000 \"lise.out\"\n-1\n");
  1605.    fprintf(output,"1 52 0.0 0.0 1000.0 1000.0 0 0 0 0 0\n");
  1606.    vector_out(output,x_factor,y_factor,0.0,0.0,
  1607.       "   0\n1 52 0.0 0.0 1000.0 1000.0 0 0 0 0 0\n   1 %4.3f %4.3f\n",
  1608.       "   1 %4.3f %4.3f\n",0);
  1609.    fprintf(output,"   0\n-1\n");
  1610.    fclose(output);
  1611.    return(0);
  1612. }
  1613.  
  1614. save_dxf_()
  1615. {
  1616. char s[80];
  1617.  
  1618.    FileSelect("Select DXF file",s);
  1619.    save_dxf(s,0,0);
  1620.    return(0);
  1621. }
  1622.  
  1623. save_dxf(s,xsize,ysize)
  1624. char *s;
  1625. int xsize, ysize;
  1626. {
  1627. FILE *output;
  1628. float x_factor, y_factor;
  1629.  
  1630.    x_factor = 0.24;
  1631.    y_factor = 0.24;
  1632.  
  1633.    output = fopen(s,"w");
  1634.    fprintf(output,"0\nSECTION\n");
  1635.    fprintf(output,"2\nENTITIES\n");
  1636.  
  1637.    vector_out(output,x_factor,y_factor,0.0,0.0,
  1638.       "0\nLINE\n8\n0\n10\n %4.3f\n20\n %4.3f\n",
  1639.       "11\n %4.3f\n21\n %4.3f\n",2);
  1640.  
  1641.    fprintf(output,"0\nENDESEC\n");
  1642.    fprintf(output,"0\nEOF\n");
  1643.  
  1644.    fclose(output);
  1645.    return(0);
  1646. }
  1647.  
  1648. save_ps_()
  1649. {
  1650. char s[80];
  1651.  
  1652.    FileSelect("Select PostScript file",s);
  1653.    save_ps(s,0,0);
  1654.    return(0);
  1655. }
  1656.  
  1657. save_ps(str,xsize,ysize)
  1658. char *str;
  1659. int xsize, ysize;
  1660. {
  1661. char s[80];
  1662. FILE *output;
  1663. float x_factor, y_factor;
  1664.  
  1665.    x_factor = (float) xsize / 100.0; if(xsize == 0) x_factor = 0.125;
  1666.    y_factor = (float) ysize / 100.0; if(ysize == 0) y_factor = 0.125;
  1667.  
  1668.    if(str[0] == 0) {
  1669.       FileSelect("Select PostScript file",s);
  1670.    } else {
  1671.       strcpy(s,str);
  1672.    }
  1673.    output = fopen(s,"w");
  1674.    fprintf(output,"%c!PS-Adobe-1.0\n",'%');
  1675.    vector_out(output, x_factor, y_factor, 10.0, 30.0,
  1676.       "0.4 setlinewidth\nstroke\n%4.2f %4.2f moveto\n",
  1677.       "%4.1f99 %4.1f99 lineto\n",1);
  1678.    fprintf(output,"0.4 setlinewidth\nstroke\nshowpage\n");
  1679.    fclose(output);
  1680.    return(0);
  1681. }
  1682.  
  1683. /* --------------------------------------------------------------------
  1684.       IFF save routinen
  1685.    -------------------------------------------------------------------- */
  1686. save_iff_()
  1687. {
  1688. char s[80];
  1689.  
  1690.    FileSelect("Select IFF file",s);
  1691.    save_iff(s);
  1692.    return(0);
  1693. }
  1694.  
  1695. save_iff(str)
  1696. char *str;
  1697. {
  1698. unsigned char *bitplane;
  1699. int n;
  1700. char  s[80];
  1701.  
  1702.    if(str[0] == 0) {
  1703.       FileSelect("Select IFF file",s);
  1704.    } else {
  1705.       strcpy(s,str);
  1706.    }
  1707.  
  1708.    x_prt_siz = 2048;
  1709.    y_prt_siz = 2048;
  1710.  
  1711.    n = (x_prt_siz * y_prt_siz) / 8;     /* length of buffer needed */
  1712.    n = n + 1024;                        /* to go shure */
  1713.    bitplane=(unsigned char *)calloc(n,sizeof(char));
  1714.    if(buf==NULL) {
  1715.       fprintf(stderr,"could not allocate shadow memory %d bytes\n",n);
  1716.       return(0);
  1717.    }
  1718.    redrawhco(bitplane);
  1719.    mem_iff(s,bitplane,x_prt_siz,y_prt_siz);
  1720.    free(bitplane);
  1721.    return(0);
  1722. }
  1723.  
  1724.  
  1725. mem_iff(name,ptr,width,height)
  1726. char name[];
  1727. unsigned char *ptr;
  1728. int width,height;
  1729. {
  1730. int output;
  1731.  static long i,j,anzcol,lenheader,lenfile,lencmap,lenbody;
  1732.  static BYTE r,g,b;
  1733.  
  1734.    output = creat(name,0);
  1735.  
  1736.  /* Initialisierung des BMHD-Chunks */
  1737.  BMHDs.Height=height;
  1738.  BMHDs.Width=width;
  1739.  BMHDs.Depth=1;
  1740.  BMHDs.compression=0;
  1741.  BMHDs.PWidth=BMHDs.Width;
  1742.  BMHDs.PHeight=BMHDs.Height;
  1743.  if(BMHDs.Height>=400) BMHDs.yaspect *= 2;
  1744.  if(BMHDs.Width>=640) BMHDs.xaspect *= 2;
  1745.  CAMGs.ViewModes= HIRES | LACE;
  1746.  /* Anzahl der Farben? */
  1747.  j=BMHDs.Depth;
  1748.  if(j>5) j=5;  /* maximal 32 Farbregister abspeichern! */
  1749.  for(i=0,anzcol=1;i<j;anzcol *= 2,++i);
  1750.  lencmap = anzcol*3;
  1751.  lenbody=BMHDs.Width/8*BMHDs.Height*BMHDs.Depth;
  1752.  lenheader=sizeof(FORMs)-8+sizeof(BMHDs)+sizeof(CAMGs)+lencmap+8+8;
  1753.  lenfile=lenheader+lenbody;
  1754.  FORMs.len=lenfile;
  1755.  /* Speichern des FORM-Chunks */
  1756.    write(output,&FORMs,sizeof(FORMs));
  1757.  /* Speichern des BMHD-Chunks */
  1758.    write(output,&BMHDs,sizeof(BMHDs));
  1759.  /* Speichern des CAMG-Chunks */
  1760.    write(output,&CAMGs,sizeof(CAMGs));
  1761.  /* Speichern des CMAP-Chunks */
  1762.    write(output,&cmap,4);
  1763.    write(output,&lencmap,4);
  1764.    lencmap+=8;
  1765.    r = 255; g = 255; b = 255;
  1766.    write(output,&r,1); write(output,&g,1); write(output,&b,1);
  1767.    r = 0; g = 0; b = 0;
  1768.    write(output,&r,1); write(output,&g,1); write(output,&b,1);
  1769.  
  1770.  /* Speichern des BODY-Chunks */
  1771.    write(output,&body,4);
  1772.    write(output,&lenbody,4);
  1773.    write(output,ptr,(width * height) >> 3);
  1774.    
  1775.    close(output);                    /* File schliessen */
  1776.    return(0);
  1777. }
  1778.  
  1779.  
  1780. main(argc,argv)
  1781. int argc;
  1782. char *argv[];
  1783. {
  1784. char z[80];
  1785. int wx = 500,
  1786.     wy = 400,
  1787.     wl = -1,
  1788.     wt = -1;
  1789.  
  1790.   _MAXSPCLEN = 16; /* prevent wasting of memory */
  1791.  
  1792.   if(checkopt(argc,argv,"-paper",z)) paper=atoi(z);
  1793.   if(checkopt(argc,argv,"-ink",z)) ink=atoi(z);
  1794.   if(checkopt(argc,argv,"-left",z)) wl=atoi(z);
  1795.   if(checkopt(argc,argv,"-top",z)) wt=atoi(z);
  1796.   if(checkopt(argc,argv,"-xsize",z)) wx = atoi(z);
  1797.   if(checkopt(argc,argv,"-ysize",z)) wy = atoi(z);
  1798.  
  1799. /* --- window geometry and name */
  1800.   my_new_window = init_window(wx,wy,"tek-Amiga",0,1);
  1801.   if(wl > 0)my_new_window->LeftEdge = wl;
  1802.   if(wt > 0)my_new_window->TopEdge = wt;
  1803.  
  1804. /* ----- initializing menus and requesters */
  1805.  
  1806.    add_item(-1,-1,NEW_MENU,"project",0,0); 
  1807.       fn_number++;
  1808.    add_item(-1,-1,MENU,"Hard Copy",0,0); 
  1809.       fn_command[fn_number] = (void *)hco_;
  1810.       fn_number++;
  1811.    add_item(-1,-1,MENU,"Save IFF",0,0); 
  1812.       fn_command[fn_number] = (void *)save_iff_;
  1813.       fn_number++;
  1814.    add_item(-1,-1,MENU,"Save HP-GL",0,0); 
  1815.       fn_command[fn_number] = (void *)save_hpgl_;
  1816.       fn_number++;
  1817.    add_item(-1,-1,MENU,"Save ADRAW",0,0); 
  1818.       fn_command[fn_number] = (void *)save_adraw_;
  1819.       fn_number++;
  1820.    add_item(-1,-1,MENU,"Save DXF",0,0); 
  1821.       fn_command[fn_number] = (void *)save_dxf_;
  1822.       fn_number++;
  1823.    add_item(-1,-1,MENU,"Save PostScript",0,0); 
  1824.       fn_command[fn_number] = (void *)save_ps_;
  1825.       fn_number++;
  1826.    add_item(-1,-1,MENU,"Clear",0,0); 
  1827.       fn_command[fn_number] = (void *)dperas;
  1828.       fn_number++;
  1829.    add_item(-1,-1,MENU,"Redraw",0,0); 
  1830.       fn_command[fn_number] = (void *)redraw2;
  1831.       fn_number++;
  1832.    add_item(-1,-1,MENU,"Cursor",0,0); 
  1833.       fn_command[fn_number] = (void *)Cursor;
  1834.       fn_number++;
  1835.    add_item(-1,-1,NEW_MENU,"colour",0,0); 
  1836.       fn_number++;
  1837.    add_item(-1,-1,MENU,"Ink +",0,0); 
  1838.       fn_command[fn_number] = (void *)InkPlus;
  1839.       fn_number++;
  1840.    add_item(-1,-1,MENU,"Ink -",0,0); 
  1841.       fn_command[fn_number] = (void *)InkMinus;
  1842.       fn_number++;
  1843.    add_item(-1,-1,MENU,"Paper +",0,0); 
  1844.       fn_command[fn_number] = (void *)PaperPlus;
  1845.       fn_number++;
  1846.    add_item(-1,-1,MENU,"Paper -",0,0); 
  1847.       fn_command[fn_number] = (void *)PaperMinus;
  1848.       fn_number++;
  1849.  
  1850.    tekname = (unsigned char *) malloc(80);
  1851.    primaryfont = (unsigned char *) malloc(80);
  1852.    secondaryfont = (unsigned char *) malloc(80);
  1853.    defaultprinter = (unsigned char *) malloc(80);
  1854.    systemcommand = (unsigned char *) malloc(80);
  1855.    bufferfile = (unsigned char *) malloc(80);
  1856.  
  1857.    teknewopen();
  1858.    if(checkopt(argc,argv,"-buf",z)) {
  1859.       strcpy(bufferfile,z);
  1860.       if(bufferfile[0] > '9') {
  1861.          bufferonfile = TRUE;
  1862.       } else {
  1863.          bufferonfile = FALSE;
  1864.          bufsiz = atoi(bufferfile);
  1865.       }
  1866.    }
  1867.  
  1868.    if(checkopt(argc,argv,"-nohco",z)) nohco=TRUE;
  1869.  
  1870.    if(bufferonfile) {
  1871.       fdbuffer = creat(bufferfile,0666);
  1872.       close(fdbuffer);
  1873.       fdbuffer = open(bufferfile,2,0666);
  1874.    } else {
  1875.       if(bufsiz < 1024) {
  1876.          nohco = TRUE;
  1877.       } else {
  1878.          if(!nohco) buf = (unsigned char *)malloc(bufsiz+10);
  1879.       }
  1880.    }
  1881.  
  1882.    font_1 = (unsigned char *) malloc(16384);
  1883.    font_2 = (unsigned char *) malloc(16384);
  1884.    prop_1 = (int *) calloc(256,sizeof(int));
  1885.    prop_2 = (int *) calloc(256,sizeof(int));
  1886.  
  1887.    fontfile = font_2; proptab = prop_2;
  1888.    readfont(secondaryfont);
  1889.    fontdesc_2 = fontdesc;
  1890.    fontheight_2 = fontheight;
  1891.    fontwidth_2 = fontwidth;
  1892.    firstchar_2 = firstchar;
  1893.    offschar_2 = offschar;
  1894.  
  1895.    fontfile = font_1; proptab = prop_1;
  1896.    readfont(primaryfont);
  1897.    fontdesc_1 = fontdesc;
  1898.    fontheight_1 = fontheight;
  1899.    fontwidth_1 = fontwidth;
  1900.    firstchar_1 = firstchar;
  1901.    offschar_1 = offschar;
  1902.  
  1903.   my_window = (struct Window *) OpenWindow( my_new_window );
  1904.   SetMenuStrip( my_window, menu_bar );
  1905.   if(my_window == NULL)
  1906.   {
  1907.     CloseLibrary( IntuitionBase );
  1908.     exit();  
  1909.   }
  1910.   rp = my_window->RPort;
  1911.   SetDrMd(rp,JAM1);          /* Drawmode setzen */
  1912.   SetAPen(rp,2);             /* Farbregister zum Zeichnen setzen */
  1913.  
  1914.   fin_flg = FALSE;
  1915.   MainLoop2(my_window);
  1916.  
  1917.   remove_tek();
  1918.   CloseLibrary(IntuitionBase);
  1919.   CloseLibrary(GfxBase);
  1920.  
  1921.   free(font_1); free(font_2); free(prop_1); free(prop_2);
  1922.   free(tekname); free(primaryfont); free(secondaryfont);
  1923.   free(defaultprinter); free(systemcommand); free(bufferfile);
  1924.   if(bufferonfile) {
  1925.       close(fdbuffer);
  1926.       unlink(bufferfile);
  1927.    } else {
  1928.      if (!nohco) free(buf);
  1929.   }
  1930.  
  1931.   exit(0);
  1932. }
  1933.  
  1934. MainLoop2(window)
  1935. struct Window *window;
  1936. {
  1937. int wx,wy;
  1938. ULONG class;
  1939. ULONG code;
  1940. struct IntuiMessage *my_message;
  1941. struct Gadget *my_gadget;
  1942.  
  1943.   close_me = FALSE;
  1944.   wx = window->Width;
  1945.   wy = window->Height;
  1946.   while( close_me == FALSE )
  1947.   {
  1948.     Delay(20);
  1949.  
  1950.     while(my_message=(struct IntuiMessage *)GetMsg(window->UserPort))
  1951.     {
  1952.       class = my_message->Class;      /* Save the IDCMP flag. */
  1953.       code = my_message->Code;
  1954.       ReplyMsg( my_message );
  1955.       switch( class )
  1956.       {
  1957.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  1958.                close_me=TRUE;
  1959.                break;
  1960.              
  1961.         case GADGETDOWN:   /* The user has selected a gadget: */
  1962.                close_me=read_gadgets(my_message,window);
  1963.                break;
  1964.  
  1965.         case GADGETUP:     /* The user has released a gadget: */
  1966.               my_gadget = (struct Gadget *) my_message->IAddress;
  1967.               if(my_gadget->GadgetType == STRGADGET) {
  1968.                   close_me=read_gadgets(my_message,window);
  1969.                }
  1970.                break;
  1971.         case MENUPICK:
  1972.                CallBack_menu(code);
  1973.                break;
  1974.       }
  1975.       if((wx != window->Width) || (wy != window->Height)) {
  1976.          redraw2();
  1977.          wx = window->Width;
  1978.          wy = window->Height;
  1979.       }
  1980.     }
  1981.   }
  1982.   close_gfx(window);
  1983.   return(0);
  1984. }
  1985.  
  1986. system2(str)
  1987. char *str;
  1988. {
  1989. struct FileHandle *input, *output;
  1990.  
  1991.    if(instr("IGNORE",str) >= 0) return(0);
  1992.  
  1993.    input = (struct FileHandle *) Open("nil:",MODE_OLDFILE);
  1994.    output = (struct FileHandle *) Open("nil:",MODE_NEWFILE);
  1995.    Execute(str,input,output);
  1996.    Close(input);
  1997.    Close(output);
  1998.    return(0);
  1999. }
  2000.